package com.mrper.util.share;

import android.app.Activity;

import com.google.gson.Gson;
import com.miguo.framework.view.LoadingDialog;
import com.miguo.plugins.share.ShareQQActivity;
import com.miguo.ui.R;
import com.miguo.utils.WebJsApi;
import com.mrper.model.share.ShareContentWithImageInfoEntry;
import com.mrper.util.DialogUtil;
import com.mrper.util.ToastUtil;

import org.json.JSONObject;

/**
 * Created by admin on 2016/4/27.
 */
public class ShareUtil {

    /**  分享到微信  **/
    public static final int SHARE_TYPE_WECHAT = 0x001;
    /** 分享到QQ  **/
    public static final int SHARE_TYPE_QQ = 0x002;


    private Activity context;
    private ShareCallback shareCallback;

    public ShareUtil(Activity context){
        this.context = context;
        this.shareCallback = new ShareCallback(context);
    }

    /** 设置加载对话框 **/
    public void setLoadingDialog(LoadingDialog loadingDialog){
        if(this.shareCallback!=null)
            this.shareCallback.setLoadingDialog(loadingDialog);
    }

    /** 内部生成dialog对话框 **/
    public void initLoadingDialog(){
        if(this.shareCallback!=null){
            if(shareCallback.getLoadingDialog() == null) {
                LoadingDialog loadingDialog = new LoadingDialog(context);
                loadingDialog.setMessage(R.string.activity_goodsDetail_shareLoading);
                shareCallback.setLoadingDialog(loadingDialog);
            }
        }
    }

    /**
     * 开始分享
     * @param shareType 分享类型，{@link ShareUtil#SHARE_TYPE_WECHAT} and {@link ShareUtil#SHARE_TYPE_QQ}
     * @param shareContent 要分享的内容
     * @param scence 使用场景，微信中：1.朋友圈 2.微信好友 QQ中：1.QQ好友 2.QQ空间
     **/
    public void startShare(int shareType, ShareContentWithImageInfoEntry shareContent, int scence) {
        try {
            if (shareContent != null) {
                shareContent.scene = scence;//设置场景
                DialogUtil.showDialog(shareCallback.getLoadingDialog());//显示分享对话框提示
                JSONObject shareInformation = new JSONObject(new Gson().toJson(shareContent));
                if (shareType == SHARE_TYPE_WECHAT)
                    WebJsApi.shareWX(context, shareInformation, shareCallback);
                else if (shareType == SHARE_TYPE_QQ)
                    ShareQQActivity.invoke(context, shareInformation, shareCallback);
            } else {
                ToastUtil.showShortToast(context, R.string.activity_goodsDetail_shareContentIsNotCompleted);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
